package com.hjq.mallware.service.impl;

import com.hjq.common.constants.WareConstants;
import com.hjq.mallware.entity.PurchaseDetailEntity;
import com.hjq.mallware.entity.vo.MergeFormVO;
import com.hjq.mallware.entity.vo.PurchaseDetailDoneVO;
import com.hjq.mallware.entity.vo.PurchaseDoneVO;
import com.hjq.mallware.service.PurchaseDetailService;
import com.hjq.mallware.service.WareSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjq.common.utils.PageUtils;
import com.hjq.common.utils.Query;

import com.hjq.mallware.dao.PurchaseDao;
import com.hjq.mallware.entity.PurchaseEntity;
import com.hjq.mallware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;


@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {
    @Autowired
    PurchaseDetailService purchaseDetailService;
    @Autowired
    WareSkuService skuService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceived(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>().eq("status",0).or().eq("status",1)
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void mergePurchase(MergeFormVO mergeFormVO) {
        Long purchaseId = mergeFormVO.getPurchaseId();
        if(purchaseId ==null){
            //新建一个采购单
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            purchaseEntity.setStatus(WareConstants.purchaseStatus.CREATED.code);

            this.save(purchaseEntity);
            //
            purchaseId = purchaseEntity.getId();
        }
        List<Long> items = mergeFormVO.getItems();
        Long finalPurchaseId = purchaseId;
        List<PurchaseDetailEntity> purchaseDetailEntities = items.stream().map(item -> {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
            detailEntity.setId(item);
            detailEntity.setPurchaseId(finalPurchaseId);
            detailEntity.setStatus(WareConstants.purchaseDetailStatus.ASSIGNED.code);
            return detailEntity;
        }).collect(Collectors.toList());
        purchaseDetailService.updateBatchById(purchaseDetailEntities);
        PurchaseEntity updateTimeEntity = new PurchaseEntity();
        updateTimeEntity.setUpdateTime(new Date());
        updateTimeEntity.setId(purchaseId);
        this.updateById(updateTimeEntity);



    }

    @Override
    @Transactional
    public void received(List<Long> ids) {
        //确认是新建或者已分配状态
        List<PurchaseEntity> purchaseEntities = ids.stream().map(this::getById)
                .filter(item-> item.getStatus() == WareConstants.purchaseStatus.CREATED.code || item.getStatus() == WareConstants.purchaseStatus.ASSIGNED.code)
                .map(om->{
                    om.setStatus(WareConstants.purchaseStatus.RECEIVED.code);
                    om.setUpdateTime(new Date());
                    return om;
                })

                .collect(Collectors.toList());


        //改变采购单状态
        this.updateBatchById(purchaseEntities);

        //改变采购项状态
        purchaseEntities.forEach(item->{
            List<PurchaseDetailEntity> detailEntities = purchaseDetailService.listDetailByPurchaseId(item.getId());
            List<PurchaseDetailEntity> collect = detailEntities.stream().map(one -> {
                PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();
                detailEntity.setId(one.getId());
                detailEntity.setStatus(WareConstants.purchaseDetailStatus.BUYING.code);
                return detailEntity;
            }).collect(Collectors.toList());
            purchaseDetailService.updateBatchById(collect);
        });
    }

    @Override
    public void donePurchase(PurchaseDoneVO purchaseDoneVO) {


        //改变采购项状态
        boolean isSuccess=true;
        List<PurchaseDetailDoneVO> items = purchaseDoneVO.getItems();
        List<PurchaseDetailEntity> detailToUpdate = new ArrayList<>();

        for (PurchaseDetailDoneVO item: items
             ) {
            PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
            if(item.getStatus()==WareConstants.purchaseDetailStatus.HASERROR.code){

                isSuccess=false;
                purchaseDetailEntity.setStatus(WareConstants.purchaseDetailStatus.HASERROR.code);

            }else{
                purchaseDetailEntity.setStatus(WareConstants.purchaseDetailStatus.FINIFSHED.code);
                //将成功的sku入库

                PurchaseDetailEntity purchaseDetail= purchaseDetailService.getById(item.getItemId());
                skuService.addStock(purchaseDetail.getSkuId(),purchaseDetail.getWareId(),purchaseDetail.getSkuNum());


            }
            purchaseDetailEntity.setId(item.getItemId());
            detailToUpdate.add(purchaseDetailEntity);
        }
        purchaseDetailService.updateBatchById(detailToUpdate);

        //改变采购单状态
        Long purchaseId = purchaseDoneVO.getId();
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(purchaseId);
        if(isSuccess){
            purchaseEntity.setStatus(WareConstants.purchaseStatus.FINIFSHED.code);
        }
        else {
            purchaseEntity.setStatus(WareConstants.purchaseStatus.HASERROR.code);
        }
        purchaseEntity.setUpdateTime(new Date());
        this.updateById(purchaseEntity);



    }

}